package br.edu.ufcg.ga.parser.args;

import junit.framework.TestCase;
import junit.textui.TestRunner;

import org.apache.commons.cli.ParseException;
import org.jgap.InvalidConfigurationException;

/**
 * 
 * @author jaindsonvs
 * 
 */
public class ArgsParserTest extends TestCase {

	private ArgsParser ap;

	public void setUp() throws Exception {
		this.ap = new ArgsParser();
		this.ap.init();
	}

	public void testNSbestChromosomesSelectorRightArgs() throws ParseException, InvalidConfigurationException {
		this.ap.parseArgs(getArgsWithRequiredOptions("-NS_BCS -BCS_SR=0.9 -BCS_DCA=true"));
		this.ap
				.parseArgs(getArgsWithRequiredOptions("--NSbestChromosomesSelector --BCSselectionRate=0.9 --BCSdoubletteChromosomesAllowed=true"));
	}

	public void testNSbestChromosomesSelectorMissingArgBCS_SR() throws ParseException {
		try {
			this.ap.parseArgs(getArgsWithRequiredOptions("-NS_BCS -BCS_DCA=true"));
			fail("Error! Test passed with missing arg <BCS_SR>");
		} catch (Exception e) {
		}
	}

	public void testNSbestChromosomesSelectorMissingArgBCS_DCA() throws ParseException {
		try {
			this.ap.parseArgs(getArgsWithRequiredOptions("-NS_BCS -BCS_SR=0.9"));
			fail("Error! Test passed with missing arg <BCS_DCA>");
		} catch (Exception e) {
		}
	}

	public void testNSweightedRouletteSelectorRightArgs() throws ParseException, InvalidConfigurationException {
		this.ap.parseArgs(getArgsWithRequiredOptions("-WRS"));
		this.ap.parseArgs(getArgsWithRequiredOptions("--NSweightedRouletteSelector"));
	}

	public void testNStournamentSelectorRightArgs() throws ParseException, InvalidConfigurationException {
		this.ap.parseArgs(getArgsWithRequiredOptions("-NS_TS -TS_TS=3 -TS_SR=0.5"));
		this.ap.parseArgs(getArgsWithRequiredOptions("--NStournamentSelector --TStournamentSize=3 --TSselectionRate=0.5"));
	}

	public void testNStournamentSelectorMissingArgTS_TS() throws ParseException {
		try {
			this.ap.parseArgs(getArgsWithRequiredOptions("-NS_TS -TS_SR=0.5"));
			fail("Error! Test passed with missing arg <TS_TS>");
		} catch (Exception e) {
		}
	}

	public void testNStournamentSelectorMissingArgTS_SR() throws ParseException {
		try {
			this.ap.parseArgs(getArgsWithRequiredOptions("-NS_TS -TS_TS=3"));
			fail("Error! Test passed with missing arg <TS_SR>");
		} catch (Exception e) {
		}
	}

	public void testGOgreedyCrossoverOperatorRightArgs() throws ParseException, InvalidConfigurationException {
		this.ap.parseArgs(getArgsWithRequiredOptions("-GO_GCO"));
		this.ap.parseArgs(getArgsWithRequiredOptions("--GOgreedyCrossoverOperator"));
	}

	public void testGOswappingMutationOperatorRightArgs() throws ParseException, InvalidConfigurationException {
		this.ap.parseArgs(getArgsWithRequiredOptions("-GO_SMO -SMO_MR=0.6"));
		this.ap.parseArgs(getArgsWithRequiredOptions("--GOswappingMutationOperator --SMOmutationRate=0.6"));
	}

	public void testGOswappingMutationOperatorMissingArgSMO_MR() throws ParseException {
		try {
			this.ap.parseArgs(getArgsWithRequiredOptions("-GO_SMO"));
			fail("Error! Test passed with missing arg <SMO_MR>");
		} catch (Exception e) {
		}
	}

	public void testMixNSsAndGOs() throws ParseException, InvalidConfigurationException {
		this.ap.parseArgs(getArgsWithRequiredOptions("-NS_BCS -BCS_SR=0.9 -BCS_DCA=true -GO_GCO -GO_SMO -SMO_MR=0.6"));
	}

	private String[] getArgsWithRequiredOptions(String args) {
		return ("-SO_PS=100 -SO_MG=0 -SO_MRBF=0 -SO_CD=false " + args).replaceAll("=", " ").split(" ");
	}

	public static void main(String[] args) {

		TestRunner.run(ArgsParserTest.class);

	}

}
